﻿using System;
using System.Data;
using System.Collections.Generic;
using Chenxin.Lib.Hr.Entity;
using Chenxin.Lib.Utility;

namespace Chenxin.Lib.Hr.BLL
{
    /// <summary>
    /// 业务逻辑类Base_Permit_RoleDefine 的摘要说明。
    /// </summary>
    public class Base_Permit_RoleDefine
    {
        //Cache服务类
        private Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.HR);

        public Base_Permit_RoleDefine()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid RoleId)
        {
            return Chenxin.Lib.Hr.DAL.Base_Permit_RoleDefine.Exists(RoleId);
        }


        /// <summary>
        /// 复制角色
        /// </summary>
        public void Copy(Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine model, string roleid)
        {
            Chenxin.Lib.Hr.DAL.Base_Permit_RoleDefine.Add(model);
            Chenxin.Lib.Hr.DAL.Base_RolePermitionRelationInfo.Copy(model, roleid);
            //查找Cache的角色列表键
            string strCacheKey = Chenxin.Lib.Cache.CacheDataType.HR_ALLROLES;
            //删除权限列表
            cs.Delete(strCacheKey);
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine model)
        {
            Chenxin.Lib.Hr.DAL.Base_Permit_RoleDefine.Add(model);
            //查找Cache的角色列表键
            string strCacheKey = Chenxin.Lib.Cache.CacheDataType.HR_ALLROLES;
            //删除权限列表
            cs.Delete(strCacheKey);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine model)
        {
            Chenxin.Lib.Hr.DAL.Base_Permit_RoleDefine.Update(model);
            //Cache的角色列表键
            string strKeyAll = Chenxin.Lib.Cache.CacheDataType.HR_ALLROLES;
            //单个角色
            string strKeySingle = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_SINGLEPERMIT, model.RoleId.ToString());
            //删除角色列表
            cs.Delete(strKeyAll);
            //删除单个角色
            cs.Delete(strKeySingle);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(Guid RoleId)
        {

            Chenxin.Lib.Hr.DAL.Base_Permit_RoleDefine.Delete(RoleId);
            //Cache的角色列表键
            string strKeyAll = Chenxin.Lib.Cache.CacheDataType.HR_ALLROLES;
            //单个角色
            string strKeySingle = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_SINGLEPERMIT, RoleId.ToString());
            //删除角色列表
            cs.Delete(strKeyAll);
            //删除单个角色
            cs.Delete(strKeySingle);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine GetModel(Guid RoleId)
        {
            //角色实体
            Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine entBasePermitRoleDefine;
            //单个角色键
            string strKeySingle = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_SINGLEPERMIT, RoleId.ToString());
            //从Cache中获取单个角色
            entBasePermitRoleDefine = cs.GetObject<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine>(strKeySingle);
            //未命中
            if (!cs.Hit)
            {
                //从数据库中取得单个角色
                entBasePermitRoleDefine = Chenxin.Lib.Hr.DAL.Base_Permit_RoleDefine.GetModel(RoleId);
                //添加到Cache中
                cs.Set(strKeySingle, entBasePermitRoleDefine);
            }
            return entBasePermitRoleDefine;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return Chenxin.Lib.Hr.DAL.Base_Permit_RoleDefine.GetList(strWhere);
        }

        public DataSet GetPaginationData(Dictionary<string, string> dicParam)
        {
            string TableNm = "Base_Permit_RoleDefine";
            string SelectField = "RoleId,RoleName,CityId,CityName,CompanyId,CompanyName";
            string TableField = "RoleId,RoleName,CityId,CityName,CompanyId,CompanyName";
            string OrderField = "CityName,CompanyName,RoleName";
            int PageIndex = 1;
            int PageSize = 10;
            System.Text.StringBuilder StrWhere = new System.Text.StringBuilder();

            if (dicParam.ContainsKey("PageIndex"))
            {
                PageIndex = Convert.ToInt32(dicParam["PageIndex"]);
            }
            if (dicParam.ContainsKey("PageSize"))
            {
                PageSize = Convert.ToInt32(dicParam["PageSize"]);
            }

            //权限判断
            if (dicParam.ContainsKey("Permit"))
            {
                string strPermit = dicParam["Permit"];
                //全权限
                if (strPermit == Constants.Permit_ViewAllRole)
                {
                    StrWhere.Append("1=1");
                }
                //城市权限
                else if (strPermit == Constants.Permit_ViewCityRole)
                {
                    StrWhere.AppendFormat(" CityId='{0}'", dicParam["CurCityId"]);
                }
                //公司权限
                else if (strPermit == Constants.Permit_ViewCompanyRole)
                {
                    StrWhere.AppendFormat(" CompanyId='{0}'", dicParam["CurCompanyId"]);
                }
                else
                {
                    StrWhere.Append("1=0");
                }
            }
            else
            {
                StrWhere.Append("1=0");
            }
            //查看隐藏角色权限
            if (dicParam.ContainsKey("Permit_Show"))
            {
                if (dicParam["Permit_Show"] != "All")
                {
                    StrWhere.AppendFormat(" And IsShow= {0} ", "1");
                }
            }
            else
            {
                StrWhere.AppendFormat(" And IsShow= {0} ", "1");
            }

            //城市
            if (dicParam.ContainsKey("CityId"))
            {
                StrWhere.AppendFormat(" And CityId='{0}'", dicParam["CityId"]);
            }
            //公司
            if (dicParam.ContainsKey("CompanyId"))
            {
                StrWhere.AppendFormat(" And CompanyId='{0}'", dicParam["CompanyId"]);
            }
            //显示状态
            if (dicParam.ContainsKey("showStatus"))
            {
                StrWhere.AppendFormat(" And IsShow='{0}'", dicParam["showStatus"]);
            }

            return Chenxin.Lib.Hr.DAL.Base_Permit_RoleDefine.GetPaginationData(TableNm, SelectField, TableField, StrWhere.ToString(), OrderField, PageIndex, PageSize, true);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine> GetModelList(string strWhere)
        {
            DataSet ds = Chenxin.Lib.Hr.DAL.Base_Permit_RoleDefine.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine> DataTableToList(DataTable dt)
        {
            List<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine> modelList = new List<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine();
                    if (dt.Rows[n]["RoleId"].ToString() != "")
                    {
                        model.RoleId = new Guid(dt.Rows[n]["RoleId"].ToString());
                    }
                    model.RoleName = dt.Rows[n]["RoleName"].ToString();
                    if (dt.Rows[n]["CityId"].ToString() != "")
                    {
                        model.CityId = new Guid(dt.Rows[n]["CityId"].ToString());
                    }
                    model.CityName = dt.Rows[n]["CityName"].ToString();
                    if (dt.Rows[n]["CompanyId"].ToString() != "")
                    {
                        model.CompanyId = new Guid(dt.Rows[n]["CompanyId"].ToString());
                    }
                    model.CompanyName = dt.Rows[n]["CompanyName"].ToString();
                    if (dt.Rows[n]["IsShow"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsShow"].ToString() == "1") || (dt.Rows[n]["IsShow"].ToString().ToLower() == "true"))
                        {
                            model.IsShow = true;
                        }
                        else
                        {
                            model.IsShow = false;
                        }
                    }
                    if (dt.Rows[n]["DefRole"].ToString() != "")
                    {
                        if ((dt.Rows[n]["DefRole"].ToString() == "1") || (dt.Rows[n]["DefRole"].ToString().ToLower() == "true"))
                        {
                            model.DefRole = true;
                        }
                        else
                        {
                            model.DefRole = false;
                        }
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine> GetAllList()
        {
            //Cache的角色列表键
            string strKeyAll = Chenxin.Lib.Cache.CacheDataType.HR_ALLROLES;
            List<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine> lsAllRoles;
            //Cache中查找
            lsAllRoles = cs.GetList<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine>(strKeyAll);
            //为命中
            if (!cs.Hit)
            {
                DataSet ds = GetList("");
                lsAllRoles = DataTableToList(ds.Tables[0]);
                //添加到Cache中
                cs.Set(strKeyAll, lsAllRoles);
            }
            return lsAllRoles;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine> GetListByCity(Guid CityId)
        {
            string strCacheKey;
            string strWhere = "";
            List<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine> lsAllRoles;
            if (CityId.Equals(Guid.Empty))
            {
                strCacheKey = Chenxin.Lib.Cache.CacheDataType.HR_ALLROLES;
            }
            else
            {
                strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_CITYROLES);
                strWhere = string.Format(" CityId = '{0}'",CityId.ToString());
            }

            //Cache中查找
            lsAllRoles = cs.GetList<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine>(strCacheKey);
            //为命中
            if (!cs.Hit)
            {
                DataSet ds = GetList(strWhere);
                lsAllRoles = DataTableToList(ds.Tables[0]);
                //添加到Cache中
                cs.Set(strCacheKey, lsAllRoles);
            }
            return lsAllRoles;
        }

        #endregion  成员方法
    }
}

